﻿
-- =============================================
-- Author:		zengfei
-- Create date: 02m/23/2012
-- Description:	操作应付应收表
-- =============================================
Create PROCEDURE [dbo].[Usp_ModifyAccountsPayable]
	@p_Action	char,				-- A:新增, C:修改, D:删除
	@p_BillNo	varchar(30),
	@p_Category	int,
	@p_PayDate	datetime,
	@p_Employee	varchar(20),
	@p_ReferenceNo	varchar(30),
	@p_Amount	numeric(15,2),
	@p_Account	varchar(30),
	@p_EntryDate	datetime,
	@p_Remark	varchar(100),
	@p_RecordID	int out,
	@p_ErrMsg	varchar(100) out,
	@p_RFlag	smallint out
AS
if	@p_Action = 'A'
begin
	if	exists (select * from AccountsPayable where BillNo = @p_BillNo)
	begin
		raiserror('应付应收单据编号[%s]重复!', 16, 1, @p_BillNo)
		goto ErrHandle
	end
	
	insert into AccountsPayable (BillNo, Category, PayDate, Employee, ReferenceNo, Amount, Account, EntryDate, Remark)
		values (@p_BillNo, @p_Category, @p_PayDate, @p_Employee, @p_ReferenceNo, @p_Amount, @p_Account, @p_EntryDate, @p_Remark)
	
	if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle
	
	select @p_RecordID = @@IDENTITY 
end
else if	@p_Action = 'C'
begin
	update AccountsPayable set Category = @p_Category, 
						PayDate = @p_PayDate, 
						Employee = @p_Employee,
						ReferenceNo = @p_ReferenceNo,
						Amount = @p_Amount,
						Account = @p_Account,
						EntryDate = @p_EntryDate,
						Remark = @p_Remark 
					where RecordID = @p_RecordID 
	
	if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle
end
else if	@p_Action = 'D'
begin
	Delete from AccountsPayable where RecordID = @p_RecordID 
	
	if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle
end

set @p_RFlag = 1

ErrHandle:
set @p_RFlag = -1

